/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controladoresJPA;

import controladoresJPA.exceptions.IllegalOrphanException;
import controladoresJPA.exceptions.NonexistentEntityException;
import controladoresJPA.exceptions.RollbackFailureException;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import modelo.Usuario;
import modelo.OperarConsumoIngreso;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;
import modelo.OperarConsumo;
import modelo.OperarConsumoConsumible;

/**
 *
 * @author Francisco
 */
public class OperarConsumoJpaController implements Serializable {

    public OperarConsumoJpaController(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }
    private UserTransaction utx = null;
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(OperarConsumo operarConsumo) throws RollbackFailureException, Exception {
        if (operarConsumo.getOperarConsumoIngresoList() == null) {
            operarConsumo.setOperarConsumoIngresoList(new ArrayList<OperarConsumoIngreso>());
        }
        if (operarConsumo.getOperarConsumoConsumibleList() == null) {
            operarConsumo.setOperarConsumoConsumibleList(new ArrayList<OperarConsumoConsumible>());
        }
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            Usuario idUsuario = operarConsumo.getIdUsuario();
            if (idUsuario != null) {
                idUsuario = em.getReference(idUsuario.getClass(), idUsuario.getIdUsuario());
                operarConsumo.setIdUsuario(idUsuario);
            }
            List<OperarConsumoIngreso> attachedOperarConsumoIngresoList = new ArrayList<OperarConsumoIngreso>();
            for (OperarConsumoIngreso operarConsumoIngresoListOperarConsumoIngresoToAttach : operarConsumo.getOperarConsumoIngresoList()) {
                operarConsumoIngresoListOperarConsumoIngresoToAttach = em.getReference(operarConsumoIngresoListOperarConsumoIngresoToAttach.getClass(), operarConsumoIngresoListOperarConsumoIngresoToAttach.getIdOperarConsumoIngreso());
                attachedOperarConsumoIngresoList.add(operarConsumoIngresoListOperarConsumoIngresoToAttach);
            }
            operarConsumo.setOperarConsumoIngresoList(attachedOperarConsumoIngresoList);
            List<OperarConsumoConsumible> attachedOperarConsumoConsumibleList = new ArrayList<OperarConsumoConsumible>();
            for (OperarConsumoConsumible operarConsumoConsumibleListOperarConsumoConsumibleToAttach : operarConsumo.getOperarConsumoConsumibleList()) {
                operarConsumoConsumibleListOperarConsumoConsumibleToAttach = em.getReference(operarConsumoConsumibleListOperarConsumoConsumibleToAttach.getClass(), operarConsumoConsumibleListOperarConsumoConsumibleToAttach.getIdOperarConsumoConsumible());
                attachedOperarConsumoConsumibleList.add(operarConsumoConsumibleListOperarConsumoConsumibleToAttach);
            }
            operarConsumo.setOperarConsumoConsumibleList(attachedOperarConsumoConsumibleList);
            em.persist(operarConsumo);
            if (idUsuario != null) {
                idUsuario.getOperarConsumoList().add(operarConsumo);
                idUsuario = em.merge(idUsuario);
            }
            for (OperarConsumoIngreso operarConsumoIngresoListOperarConsumoIngreso : operarConsumo.getOperarConsumoIngresoList()) {
                OperarConsumo oldIdOperarConsumoOfOperarConsumoIngresoListOperarConsumoIngreso = operarConsumoIngresoListOperarConsumoIngreso.getIdOperarConsumo();
                operarConsumoIngresoListOperarConsumoIngreso.setIdOperarConsumo(operarConsumo);
                operarConsumoIngresoListOperarConsumoIngreso = em.merge(operarConsumoIngresoListOperarConsumoIngreso);
                if (oldIdOperarConsumoOfOperarConsumoIngresoListOperarConsumoIngreso != null) {
                    oldIdOperarConsumoOfOperarConsumoIngresoListOperarConsumoIngreso.getOperarConsumoIngresoList().remove(operarConsumoIngresoListOperarConsumoIngreso);
                    oldIdOperarConsumoOfOperarConsumoIngresoListOperarConsumoIngreso = em.merge(oldIdOperarConsumoOfOperarConsumoIngresoListOperarConsumoIngreso);
                }
            }
            for (OperarConsumoConsumible operarConsumoConsumibleListOperarConsumoConsumible : operarConsumo.getOperarConsumoConsumibleList()) {
                OperarConsumo oldIdOperarConsumoOfOperarConsumoConsumibleListOperarConsumoConsumible = operarConsumoConsumibleListOperarConsumoConsumible.getIdOperarConsumo();
                operarConsumoConsumibleListOperarConsumoConsumible.setIdOperarConsumo(operarConsumo);
                operarConsumoConsumibleListOperarConsumoConsumible = em.merge(operarConsumoConsumibleListOperarConsumoConsumible);
                if (oldIdOperarConsumoOfOperarConsumoConsumibleListOperarConsumoConsumible != null) {
                    oldIdOperarConsumoOfOperarConsumoConsumibleListOperarConsumoConsumible.getOperarConsumoConsumibleList().remove(operarConsumoConsumibleListOperarConsumoConsumible);
                    oldIdOperarConsumoOfOperarConsumoConsumibleListOperarConsumoConsumible = em.merge(oldIdOperarConsumoOfOperarConsumoConsumibleListOperarConsumoConsumible);
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(OperarConsumo operarConsumo) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            OperarConsumo persistentOperarConsumo = em.find(OperarConsumo.class, operarConsumo.getIdOperarConsumo());
            Usuario idUsuarioOld = persistentOperarConsumo.getIdUsuario();
            Usuario idUsuarioNew = operarConsumo.getIdUsuario();
            List<OperarConsumoIngreso> operarConsumoIngresoListOld = persistentOperarConsumo.getOperarConsumoIngresoList();
            List<OperarConsumoIngreso> operarConsumoIngresoListNew = operarConsumo.getOperarConsumoIngresoList();
            List<OperarConsumoConsumible> operarConsumoConsumibleListOld = persistentOperarConsumo.getOperarConsumoConsumibleList();
            List<OperarConsumoConsumible> operarConsumoConsumibleListNew = operarConsumo.getOperarConsumoConsumibleList();
            List<String> illegalOrphanMessages = null;
            for (OperarConsumoIngreso operarConsumoIngresoListOldOperarConsumoIngreso : operarConsumoIngresoListOld) {
                if (!operarConsumoIngresoListNew.contains(operarConsumoIngresoListOldOperarConsumoIngreso)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain OperarConsumoIngreso " + operarConsumoIngresoListOldOperarConsumoIngreso + " since its idOperarConsumo field is not nullable.");
                }
            }
            for (OperarConsumoConsumible operarConsumoConsumibleListOldOperarConsumoConsumible : operarConsumoConsumibleListOld) {
                if (!operarConsumoConsumibleListNew.contains(operarConsumoConsumibleListOldOperarConsumoConsumible)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain OperarConsumoConsumible " + operarConsumoConsumibleListOldOperarConsumoConsumible + " since its idOperarConsumo field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (idUsuarioNew != null) {
                idUsuarioNew = em.getReference(idUsuarioNew.getClass(), idUsuarioNew.getIdUsuario());
                operarConsumo.setIdUsuario(idUsuarioNew);
            }
            List<OperarConsumoIngreso> attachedOperarConsumoIngresoListNew = new ArrayList<OperarConsumoIngreso>();
            for (OperarConsumoIngreso operarConsumoIngresoListNewOperarConsumoIngresoToAttach : operarConsumoIngresoListNew) {
                operarConsumoIngresoListNewOperarConsumoIngresoToAttach = em.getReference(operarConsumoIngresoListNewOperarConsumoIngresoToAttach.getClass(), operarConsumoIngresoListNewOperarConsumoIngresoToAttach.getIdOperarConsumoIngreso());
                attachedOperarConsumoIngresoListNew.add(operarConsumoIngresoListNewOperarConsumoIngresoToAttach);
            }
            operarConsumoIngresoListNew = attachedOperarConsumoIngresoListNew;
            operarConsumo.setOperarConsumoIngresoList(operarConsumoIngresoListNew);
            List<OperarConsumoConsumible> attachedOperarConsumoConsumibleListNew = new ArrayList<OperarConsumoConsumible>();
            for (OperarConsumoConsumible operarConsumoConsumibleListNewOperarConsumoConsumibleToAttach : operarConsumoConsumibleListNew) {
                operarConsumoConsumibleListNewOperarConsumoConsumibleToAttach = em.getReference(operarConsumoConsumibleListNewOperarConsumoConsumibleToAttach.getClass(), operarConsumoConsumibleListNewOperarConsumoConsumibleToAttach.getIdOperarConsumoConsumible());
                attachedOperarConsumoConsumibleListNew.add(operarConsumoConsumibleListNewOperarConsumoConsumibleToAttach);
            }
            operarConsumoConsumibleListNew = attachedOperarConsumoConsumibleListNew;
            operarConsumo.setOperarConsumoConsumibleList(operarConsumoConsumibleListNew);
            operarConsumo = em.merge(operarConsumo);
            if (idUsuarioOld != null && !idUsuarioOld.equals(idUsuarioNew)) {
                idUsuarioOld.getOperarConsumoList().remove(operarConsumo);
                idUsuarioOld = em.merge(idUsuarioOld);
            }
            if (idUsuarioNew != null && !idUsuarioNew.equals(idUsuarioOld)) {
                idUsuarioNew.getOperarConsumoList().add(operarConsumo);
                idUsuarioNew = em.merge(idUsuarioNew);
            }
            for (OperarConsumoIngreso operarConsumoIngresoListNewOperarConsumoIngreso : operarConsumoIngresoListNew) {
                if (!operarConsumoIngresoListOld.contains(operarConsumoIngresoListNewOperarConsumoIngreso)) {
                    OperarConsumo oldIdOperarConsumoOfOperarConsumoIngresoListNewOperarConsumoIngreso = operarConsumoIngresoListNewOperarConsumoIngreso.getIdOperarConsumo();
                    operarConsumoIngresoListNewOperarConsumoIngreso.setIdOperarConsumo(operarConsumo);
                    operarConsumoIngresoListNewOperarConsumoIngreso = em.merge(operarConsumoIngresoListNewOperarConsumoIngreso);
                    if (oldIdOperarConsumoOfOperarConsumoIngresoListNewOperarConsumoIngreso != null && !oldIdOperarConsumoOfOperarConsumoIngresoListNewOperarConsumoIngreso.equals(operarConsumo)) {
                        oldIdOperarConsumoOfOperarConsumoIngresoListNewOperarConsumoIngreso.getOperarConsumoIngresoList().remove(operarConsumoIngresoListNewOperarConsumoIngreso);
                        oldIdOperarConsumoOfOperarConsumoIngresoListNewOperarConsumoIngreso = em.merge(oldIdOperarConsumoOfOperarConsumoIngresoListNewOperarConsumoIngreso);
                    }
                }
            }
            for (OperarConsumoConsumible operarConsumoConsumibleListNewOperarConsumoConsumible : operarConsumoConsumibleListNew) {
                if (!operarConsumoConsumibleListOld.contains(operarConsumoConsumibleListNewOperarConsumoConsumible)) {
                    OperarConsumo oldIdOperarConsumoOfOperarConsumoConsumibleListNewOperarConsumoConsumible = operarConsumoConsumibleListNewOperarConsumoConsumible.getIdOperarConsumo();
                    operarConsumoConsumibleListNewOperarConsumoConsumible.setIdOperarConsumo(operarConsumo);
                    operarConsumoConsumibleListNewOperarConsumoConsumible = em.merge(operarConsumoConsumibleListNewOperarConsumoConsumible);
                    if (oldIdOperarConsumoOfOperarConsumoConsumibleListNewOperarConsumoConsumible != null && !oldIdOperarConsumoOfOperarConsumoConsumibleListNewOperarConsumoConsumible.equals(operarConsumo)) {
                        oldIdOperarConsumoOfOperarConsumoConsumibleListNewOperarConsumoConsumible.getOperarConsumoConsumibleList().remove(operarConsumoConsumibleListNewOperarConsumoConsumible);
                        oldIdOperarConsumoOfOperarConsumoConsumibleListNewOperarConsumoConsumible = em.merge(oldIdOperarConsumoOfOperarConsumoConsumibleListNewOperarConsumoConsumible);
                    }
                }
            }
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = operarConsumo.getIdOperarConsumo();
                if (findOperarConsumo(id) == null) {
                    throw new NonexistentEntityException("The operarConsumo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            utx.begin();
            em = getEntityManager();
            OperarConsumo operarConsumo;
            try {
                operarConsumo = em.getReference(OperarConsumo.class, id);
                operarConsumo.getIdOperarConsumo();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The operarConsumo with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<OperarConsumoIngreso> operarConsumoIngresoListOrphanCheck = operarConsumo.getOperarConsumoIngresoList();
            for (OperarConsumoIngreso operarConsumoIngresoListOrphanCheckOperarConsumoIngreso : operarConsumoIngresoListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This OperarConsumo (" + operarConsumo + ") cannot be destroyed since the OperarConsumoIngreso " + operarConsumoIngresoListOrphanCheckOperarConsumoIngreso + " in its operarConsumoIngresoList field has a non-nullable idOperarConsumo field.");
            }
            List<OperarConsumoConsumible> operarConsumoConsumibleListOrphanCheck = operarConsumo.getOperarConsumoConsumibleList();
            for (OperarConsumoConsumible operarConsumoConsumibleListOrphanCheckOperarConsumoConsumible : operarConsumoConsumibleListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This OperarConsumo (" + operarConsumo + ") cannot be destroyed since the OperarConsumoConsumible " + operarConsumoConsumibleListOrphanCheckOperarConsumoConsumible + " in its operarConsumoConsumibleList field has a non-nullable idOperarConsumo field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Usuario idUsuario = operarConsumo.getIdUsuario();
            if (idUsuario != null) {
                idUsuario.getOperarConsumoList().remove(operarConsumo);
                idUsuario = em.merge(idUsuario);
            }
            em.remove(operarConsumo);
            utx.commit();
        } catch (Exception ex) {
            try {
                utx.rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<OperarConsumo> findOperarConsumoEntities() {
        return findOperarConsumoEntities(true, -1, -1);
    }

    public List<OperarConsumo> findOperarConsumoEntities(int maxResults, int firstResult) {
        return findOperarConsumoEntities(false, maxResults, firstResult);
    }

    private List<OperarConsumo> findOperarConsumoEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(OperarConsumo.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public OperarConsumo findOperarConsumo(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(OperarConsumo.class, id);
        } finally {
            em.close();
        }
    }

    public int getOperarConsumoCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<OperarConsumo> rt = cq.from(OperarConsumo.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
